package com.bookRecommend.controller;

import com.bookRecommend.commons.ResponseUtils;
import com.bookRecommend.model.Type;
import com.bookRecommend.service.ITypeService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/type")
public class TypeController {

    @Resource
    ITypeService service;

    @RequestMapping
    public ResponseUtils findAll(Integer pageNum, Integer size) {
        PageInfo<Type> pageInfo = service.findPage(pageNum, size);
        if (pageInfo != null) {
            return ResponseUtils.success("", pageInfo);
        }
        return ResponseUtils.failure("查询失败！");
    }

    @RequestMapping("/all")
    public ResponseUtils findAll() {
        List<Type> types = service.findAllTypes();
        if (types != null) {
            return ResponseUtils.success("", types);
        }
        return ResponseUtils.failure("查询失败！");
    }

    @GetMapping("/{id}")
    public ResponseUtils findById(@PathVariable Integer id) {
        Type type = service.findById(id);
        if (type != null) {
            return ResponseUtils.success("", type);
        }
        return ResponseUtils.failure("查询失败！");
    }

    @RequestMapping("/modify")
    public ResponseUtils modifyType(String typeName, Integer id) {
        int flag = service.modifyById(typeName, id);
        if (flag > 0) {
            return ResponseUtils.success("修改成功！");
        }
        return ResponseUtils.failure("修改失败！");
    }

    @RequestMapping("/delete")
    public ResponseUtils removeById(Integer id) {
        int flag = service.removeById(id);
        if (flag > 0) {
            return ResponseUtils.success("删除成功！");
        }
        return ResponseUtils.failure("删除失败！");
    }

    @RequestMapping("/add")
    public ResponseUtils addType(String typeName) {
        int flag = service.addType(typeName);
        if (flag > 0) {
            return ResponseUtils.success("添加成功！");
        }
        return ResponseUtils.failure("添加失败");
    }

}
